/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phd.dupenois.changeidentifiers;

import phd.dupenois.changeidentifiers.exceptions.ChangeIdentifierLoadException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 *
 * @author mpd209
 */
public class ChangeIdentifierHolder implements Comparable{

    private String location;
    private String classname;
    private int priority;
    private double threshold;
    private double multiplier;
    private boolean redrawOnFail;
    private ChangeIdentifier instance;
    private int currentTimeStep;

    public ChangeIdentifierHolder(String location, String classname, int priority, double threshold, boolean redrawOnFail, double multiplier) throws ChangeIdentifierLoadException {
        this.location = location;
        this.classname = classname;
        this.priority = priority;
        this.threshold = threshold;
        this.redrawOnFail = redrawOnFail;
        this.multiplier = multiplier;
        this.currentTimeStep = 0;
        setInstance();
    }

    private void setInstance() throws ChangeIdentifierLoadException {
        Class<? extends ChangeIdentifier> engineClass;
        try {
            if (getLocation().equals(".")) {
                engineClass = (Class<? extends ChangeIdentifier>) Class.forName(getClassname());
            } else {
                String cleaned = getLocation().replaceAll("\\{1}", "\\\\");
                String urlPath = "jar:file:///" + cleaned + "!/";
                URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL(urlPath)});
                engineClass = (Class<? extends ChangeIdentifier>) classLoader.loadClass(getClassname());
            }
            this.instance = engineClass.newInstance();
            this.instance.setHolder(this);
        } catch (ClassNotFoundException classNotFoundException) {
            throw new ChangeIdentifierLoadException(classNotFoundException);
        } catch (MalformedURLException malformedURLException) {
            throw new ChangeIdentifierLoadException(malformedURLException);
        } catch (InstantiationException instantiationException) {
            throw new ChangeIdentifierLoadException(instantiationException);
        } catch (IllegalAccessException illegalAccessException) {
            throw new ChangeIdentifierLoadException(illegalAccessException);
        }
    }

    /**
     * @return the location
     */
    public String getLocation() {
        return location;
    }

    /**
     * @return the classname
     */
    public String getClassname() {
        return classname;
    }

    /**
     * @return the priority
     */
    public int getPriority() {
        return priority;
    }

    /**
     * @return the threshold
     */
    public double getThreshold() {
        return threshold;
    }

    /**
     * @return the instance
     */
    public ChangeIdentifier getInstance() {
        return instance;
    }

    /**
     * @return the redrawOnFail
     */
    public boolean isRedrawOnFail() {
        return redrawOnFail;
    }
    public final void setTimeStep(int step){
        this.currentTimeStep = step;
    }
    public final void incrementTimeStep(){
        this.currentTimeStep++;
    }
    protected final int getTimeStep(){
        return this.currentTimeStep;
    }
    public int compareTo(Object arg0) {
        if(!(arg0 instanceof ChangeIdentifierHolder)) return -1;
        if(arg0 == this) return 0;
        ChangeIdentifierHolder h = (ChangeIdentifierHolder) arg0;
        return this.priority - h.getPriority();
    }

    /**
     * @return the multiplier
     */
    public double getMultiplier() {
        return multiplier;
    }

}
